package com.tencent.org.controller;

import com.tencent.basic.util.AjaxResult;
import com.tencent.basic.util.PageList;
import com.tencent.org.domain.Emp;
import com.tencent.org.query.EmpQuery;
import com.tencent.org.service.EmpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(value = "员工接口类",description = "员工接口类")
@RestController // Response + Controller
@RequestMapping("/emp")
public class EmpController {

    @Autowired
    private EmpService empService;

    @ApiOperation(value = "查找所有")
    @GetMapping
    public List<Emp> findAll(){
        return empService.findAll();
    }

    @ApiOperation(value = "通过id查找")
    @GetMapping("/{id}")
    public Emp findById(@PathVariable("id")Long id) {
        return empService.findById(id);
    }

    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            empService.del(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    @ApiOperation(value = "增加或者修改")
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Emp emp){
        try {
            if (emp.getId()==null){
                empService.add(emp);
                return new AjaxResult();
            }else {
                empService.update(emp);
                return new AjaxResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "操作失败");
        }
    }

    @ApiOperation(value = "分页接口")
    @PostMapping
    public PageList<Emp> queryPage(@RequestBody EmpQuery empQuery){
        return empService.queryPage(empQuery);
    }

    @ApiOperation(value = "批量删除")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody List<Long> ids){
        try {
            empService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false, "删除失败");
        }
    }

    @ApiOperation(value = "查询部门树")
    @GetMapping("/empDepartmentTree")
    public List<Emp> departmentTree(){
        return empService.empDepartmentTree();
    }
}
